<?php

namespace app\admin\controller\home;

use app\common\controller\Backend;
use think\Db;

/**
 * OA流转设置
 *
 * @icon fa fa-circle-o
 */
class Oa extends Backend
{

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Projects;

        $admin_arr = Db::table('fa_admin')
            ->where('status','normal')
            ->field('id,nickname')
            ->select();
        $this->view->assign("arr", $admin_arr);
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->paginate($limit);

            if (!empty($list->items())){
                foreach ($list->items() as $k=>$v){
                    $list->items()[$k]['check_name'] = '';
                    $admin_arr = Db::table('fa_oa_examine')
                        ->alias('a')
                        ->field('a.id,b.nickname')
                        ->join('fa_admin b','a.admin_id=b.id','left')
                        ->where('a.oa_id',$v->id)
                        ->select();

                    if (!empty($admin_arr)){
                        foreach ($admin_arr as $kk=>$vv){
                            if ($kk == 0){
                                $list->items()[$k]['check_name'] .= $vv['nickname'];
                            }else{
                                $list->items()[$k]['check_name'] .= '、'.$vv['nickname'];
                            }

                        }
                    }
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }


    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $ids = [];
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");

            $ids_arr = array_merge($params['ids']);

            if (count($ids_arr) != count(array_unique($ids_arr))) {
                $this->error(__('审批人不能重复'));
            }

            //删除之前的oa流转
            $pan_arr = Db::table('fa_oa_examine')
                ->where('oa_id',$params['id'])
                ->delete();


            $arr = [];
            if ($params) {
                if (!empty($ids_arr)){
                    foreach ($ids_arr as $k=>$v){
                        $arr[$k] = [
                          'oa_id' => $params['id'],
                          'admin_id' => $v,
                          'type' => $k+1,
                          'create_time' => time(),
                        ];
                    }

                    $insert = Db::table('fa_oa_examine')
                        ->insertAll($arr);
                }else{
                    $insert = 1;
                }

                if ($insert !== false){
                    $this->success('成功');
                }else{
                    $this->error('失败');
                }

            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $oa = Db::table('fa_oa_examine')
            ->where('oa_id',$row->id)
            ->select();

        $this->view->assign("oa", $oa);
        $this->view->assign("count", count($oa));
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


}
